package com.hmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constant.MqConstants;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.ItemDoc;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    RestHighLevelClient client;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //分页查询
    @Override
    public PageDTO<Item> listPage(Integer page, Integer size) {
        IPage<Item> pages = new Page<>(page, size);
        itemMapper.selectPage(pages, null);
        List<Item> list = pages.getRecords();
        long total = pages.getTotal();
        return new PageDTO<Item>(total, list);
    }

    //根据Id查询商品
    @Override
    public Item findItemById(Long id) {
//        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Item::getId,id);
        Item item = itemMapper.selectById(id);
        return item;
    }

    //新增数据
    @Override
    public void add(Item item) {
        itemMapper.insert(item);
    }

    //上下架
    @Override
    public void sahngxiajia(Long id, Integer status) {
        LambdaUpdateWrapper<Item> wrapper = new LambdaUpdateWrapper<Item>();
        wrapper.eq(Item::getId, id)
                .set(Item::getStatus, status);
        this.update(wrapper);

        if (status == 2) {
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_DELETE_KEY, id);
            log.info("成功发送索引库通知 ，下架 对应id{}", id);
        } else if (status == 1) {
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_KEY, id);
            log.info("成功发送索引库通知 ，上架 对应id{}", id);
        }


    }


    //修改商品信息
    @Override
    public void updatee(Item item) {
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Item::getId, item.getId());
        itemMapper.update(item, wrapper);
    }

    //删除商品信息
    @Override
    public void deletete(Long id) {
        itemMapper.deleteById(id);
    }

    //上架
    @Override
    public void insert1(Integer id) {
        try {
            Item item = itemMapper.selectById(id);
            ItemDoc itemDoc = new ItemDoc(item);
            IndexRequest request = new IndexRequest("item");

            request.id(String.valueOf(itemDoc.getId()));

            request.source(JSON.toJSONString(itemDoc), XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 下架删除
    @Override
    public void delete1(Integer id) {
        try {
            DeleteRequest request = new DeleteRequest("item", String.valueOf(id));
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("删除失败");
        }
    }

}
